package com.woniuxy.creditsystem.common.mapper;



import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.ApplyInfo;

import com.woniuxy.creditsystem.common.bean.DtoApply;
import com.woniuxy.creditsystem.common.util.PageRest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
/**
 * @author ：zhengbin
 * @date ：Created in 2020/12/11
 * @description：申请表mapper
 * @modified By：
 * @version: 1.0v
 */
@Mapper
public interface ApplyMapper {


   /* @Results({
            @Result(id = true,property ="id",column ="id"),
            @Result(property ="customerInfo",column ="fk_customer_id",one =@One(select = "com.woniuxy.creditsystem.common.mapper.CustomerMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "productInfo",column = "fk_product_id",one = @One(select = "com.woniuxy.creditsystem.common.mapper.ProductMapper.CfindByID",fetchType = FetchType.LAZY)),
    })
    @Select("select id,apply_num applyNum,money,time,count,status,sum_money as sumMoney,submit_time as submitTime,audit_opinion auditOpinion,loan_alter_opinion loanAlterOpinion,fk_customer_id,fk_product_id,fk_user_id,fk_order_id from apply_info")
*/    List<DtoApply> findAll();


      List<DtoApply> findByStatus(String status);

      List<DtoApply> findNotTrail();


    List<DtoApply> findByParam(@Param("p") PageRest pageRest,@Param("d") DtoApply dtoApply);

    @Results({
            @Result(id = true,property ="id",column ="id"),
            @Result(property ="customerInfo",column ="fk_customer_id",one =@One(select = "com.woniuxy.creditsystem.common.mapper.CustomerMapper.findById",fetchType = FetchType.LAZY)),
            @Result(property = "productInfo",column = "fk_product_id",one = @One(select = "com.woniuxy.creditsystem.common.mapper.ProductMapper.CfindByID",fetchType = FetchType.LAZY)),
    })
    @Select("select * from apply_info where id=#{id}")
    ApplyInfo findByID(long id);

    @Options(
            useGeneratedKeys = true,
            keyProperty ="a.id"
    )
    @Insert("insert into  apply_info(money,time,count,status,sum_money,submit_time,audit_opinion,loan_alter_opinion,fk_customer_id,fk_product_id,fk_user_id,fk_order_id) values(#{a.money},now(),#{a.count},0,#{a.sumMoney},now(),#{a.auditOpinion},#{a.loanAlterOpinion},#{a.fkCustomerId},#{a.fkProductId},#{a.fkUserId},#{a.fkOrderId})")
    int saveApply(@Param("a") ApplyInfo applyInfo);


    @Update("<script> " +
            "update apply_info " +
            "<set>" +
            "<if test='a.money !=null'>money=#{a.money}," +
            "</if>" +
            "<if test='a.time !=null'>time=#{a.time}," +
            "</if>" +
            "<if test='a.count !=null'>count=#{a.count}," +
            "</if>" +
            "<if test='a.status !=null'>status=#{a.status}," +
            "</if>" +
            "<if test='a.sumMoney !=null'>sum_money=#{a.sumMoney}," +
            "</if>" +
            "<if test='a.submitTime !=null'>submit_time=#{a.submitTime}," +
            "</if>" +
            "<if test='a.sumMoney !=null'>sum_money=#{a.sumMoney}," +
            "</if>" +
            "<if test='a.submitTime !=null'>submit_time=#{a.submitTime}," +
            "</if>" +
            "<if test='a.auditOpinion !=null'>audit_opinion=#{a.auditOpinion}," +
            "</if>" +
            "<if test='a.loanAlterOpinion !=null'>loan_alter_opinion=#{a.loanAlterOpinion}," +
            "</if>" +
            "<if test='a.fkCustomerId!=null'>fk_customer_id=#{a.fkCustomerId}," +
            "</if>" +
            "<if test='a.fkProductId !=null'>fk_product_id=#{a.fkProductId}," +
            "</if>" +
            "<if test='a.fkUserId !=null'>fk_user_id=#{a.fkUserId}," +
            "</if>" +
            "</set>  where id=#{a.id}" +
            "</script>")
        int updateApply(@Param("a") ApplyInfo applyInfo);
}
